package com.ts.tables;

import com.ts.comm.SysColumn;
import com.ts.comm.SysJson;
import com.ts.comm.SysResult;
import com.ts.constant.ShowType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.*;

/**
 * Created by 马俊鹏 on 2017/5/9.
 */
@Controller
public class TableControl {
	@ResponseBody
	@RequestMapping(value = "/getTableData")
	public SysResult getDepartmentList() throws Exception {
		List<Map<String,Object>> multiTableList = new ArrayList<Map<String, Object>>();
		SysResult sysResult = new SysResult();
		for(int i = 0 ; i < 1000 ; i ++){
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("name","name"+String.valueOf(i));
			map.put("a","a"+String.valueOf(i));
			map.put("b","b"+String.valueOf(i));
			map.put("c","c"+String.valueOf(i));
			map.put("d","d"+String.valueOf(i));
			multiTableList.add(map);
		}
		sysResult.setRows(multiTableList);

		return sysResult;
	}

	@ResponseBody
	@RequestMapping(value = "/dynamicColumns")
	public SysResult getDynamicCols() throws Exception {
		List<Map<String,Object>> tableList = new ArrayList<Map<String, Object>>();
		SysResult sysResult = new SysResult();
		List<Object> columns=new ArrayList<Object>();
		//动态创建列
		SysColumn col=null;
		String[] colFileds=new String[]{"name","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p"};
		String[] colTitels=new String[]{"姓名","列A","列B","列C","列D","列E","列F","列G","列H","列I","列J","列K","列L","列M","列N","列O","列P"};
		for (int i = 0; i <colFileds.length ; i++) {
			col=new SysColumn();
			col.setField(colFileds[i]);
			col.setTitle(colTitels[i]);
			col.setWidth(150);
			col.setVisible(true);
			if(colFileds[i].equals("g")||colFileds[i].equals("h")){
				col.setFormat(ShowType.Fmt_Number2);
				col.setAlign("right");
			}
			columns.add(col);
		}
		sysResult.setObjOther(columns);
		//创建数据
		Map<String,Object> map =null;
		for(int i = 1 ; i <=1000 ; i ++){
			map = new HashMap<String,Object>();
			map.put("name","name"+String.valueOf(i));
			map.put("a","a"+String.valueOf(i));
			map.put("b","b"+String.valueOf(i));
			map.put("c","c"+String.valueOf(i));
			map.put("d","束带结发的所发生的及丰富的"+i);
			map.put("e","及丰富的"+i);
			map.put("f","发生的及丰富的"+i);
			map.put("g",i);
			map.put("h",i);
			map.put("i",i);
			map.put("j",i);
			map.put("k",i);
			map.put("l",i);
			map.put("m",i);
			map.put("n",i);
			map.put("o",i);
			map.put("p",i);
			tableList.add(map);
		}
		sysResult.setRows(tableList);

		return sysResult;
	}

	/**
	 * 单文件上传
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/test/subForm",method = RequestMethod.POST)
	public String uploadFile(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

		String sPwd=request.getParameter("txtPwd");
		String sRePwd=request.getParameter("txtRePwd");

		CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(
				request.getSession().getServletContext());

		HashMap<String,Object> map=new HashMap<String,Object>();
		if (multipartResolver.isMultipart(request)) {
			MultipartFile excelFile =  ((MultipartHttpServletRequest) request).getFile("txtFile");
			if(excelFile!=null && !excelFile.isEmpty()) {
				//System.out.println(excelFile.getBytes());
				excelFile.transferTo(new File("d:\\1.png"));
				map.put("msg","文件上传成功");
			}else{
				map.put("msg","文件上传失败");
			}
		}else{
			map.put("msg","文件上传失败");
		}

		String sJson=SysJson.toJson(map);
		return sJson;
	}

	/**多文件上传
	 * */
	@ResponseBody
	@RequestMapping(value = "/test/mutliForm",method = RequestMethod.POST)
	public String uploadFiles(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

		String sCode=request.getParameter("txtCode");
		StringBuilder sb=new StringBuilder();

		HashMap<String,Object> map=new HashMap<String,Object>();
		CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(
				request.getSession().getServletContext());
		if (multipartResolver.isMultipart(request)) {
			MultipartHttpServletRequest multiRequest=(MultipartHttpServletRequest)request;
			//获取multiRequest 中所有的文件名
			Iterator iter=multiRequest.getFileNames();
			while(iter.hasNext())
			{
				//一次遍历所有文件
				MultipartFile file=multiRequest.getFile(iter.next().toString());
				if(file!=null && !file.isEmpty()) {
					System.out.println(file.getName());
					sb.append(file.getName()+":上传成功！");
				}else{
					sb.append(file.getName()+":上传失败！");
				}
			}
		}
		map.put("msg","文件上传成功");
		String sJson=SysJson.toJson(map);
		return sJson;
	}
}
